using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Update.Controls
{
    /// <summary>
    /// 自定义TextBox控件，具有更窄、更符合深色主题的滚动条
    /// </summary>
    public class CustomTextBox : TextBox
    {
        // Win32 API 常量
        private const int WM_VSCROLL = 0x115;
        private const int WM_HSCROLL = 0x114;
        private const int SB_THUMBPOSITION = 4;
        private const int WM_MOUSEWHEEL = 0x20A;

        // 滚动条颜色
        private Color _thumbColor = Color.FromArgb(104, 104, 104);     // 滚动条滑块颜色
        private Color _thumbHoverColor = Color.FromArgb(158, 158, 158); // 鼠标悬停时滚动条滑块颜色
        private Color _thumbPressedColor = Color.FromArgb(0, 122, 204); // 鼠标按下时滚动条滑块颜色
        private Color _trackColor = Color.FromArgb(45, 45, 48);         // 滚动条轨道颜色
        private int _scrollBarWidth = 8;                                // 滚动条宽度

        [Category("Appearance")]
        public Color ThumbColor
        {
            get => _thumbColor;
            set
            {
                _thumbColor = value;
                Invalidate();
            }
        }

        [Category("Appearance")]
        public Color ThumbHoverColor
        {
            get => _thumbHoverColor;
            set
            {
                _thumbHoverColor = value;
                Invalidate();
            }
        }

        [Category("Appearance")]
        public Color ThumbPressedColor
        {
            get => _thumbPressedColor;
            set
            {
                _thumbPressedColor = value;
                Invalidate();
            }
        }

        [Category("Appearance")]
        public Color TrackColor
        {
            get => _trackColor;
            set
            {
                _trackColor = value;
                Invalidate();
            }
        }

        [Category("Appearance")]
        public int ScrollBarWidth
        {
            get => _scrollBarWidth;
            set
            {
                _scrollBarWidth = value;
                Invalidate();
            }
        }

        public CustomTextBox()
        {
            // 设置双缓冲以减少闪烁
            SetStyle(ControlStyles.OptimizedDoubleBuffer | 
                     ControlStyles.AllPaintingInWmPaint | 
                     ControlStyles.ResizeRedraw, true);
        }

        // 自定义滚动条绘制
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);

            // 仅当有垂直滚动条时才进行自定义绘制
            if (ScrollBars == ScrollBars.Vertical || ScrollBars == ScrollBars.Both)
            {
                if (m.Msg == WM_VSCROLL || m.Msg == WM_MOUSEWHEEL)
                {
                    // 滚动后重绘滚动条
                    RedrawScrollBar();
                }
                else if (m.Msg == 0x000F) // WM_PAINT
                {
                    // 窗口绘制时也重绘滚动条
                    RedrawScrollBar();
                }
            }
        }

        private void RedrawScrollBar()
        {
            using (Graphics g = CreateGraphics())
            {
                // 获取滚动条位置和大小
                Rectangle scrollBarRect = GetScrollBarRect();
                
                if (scrollBarRect.Width > 0 && scrollBarRect.Height > 0)
                {
                    // 绘制滚动条轨道背景
                    g.FillRectangle(new SolidBrush(_trackColor), scrollBarRect);

                    // 计算滑块位置和大小
                    Rectangle thumbRect = GetThumbRect(scrollBarRect);
                    
                    if (thumbRect.Height > 0)
                    {
                        // 绘制滑块
                        g.FillRectangle(new SolidBrush(_thumbColor), thumbRect);
                    }
                }
            }
        }

        private Rectangle GetScrollBarRect()
        {
            // 计算滚动条区域
            return new Rectangle(
                Width - _scrollBarWidth - 1, 
                1, 
                _scrollBarWidth, 
                Height - 2);
        }

        private Rectangle GetThumbRect(Rectangle scrollBarRect)
        {
            // 计算滚动条滑块位置和大小
            int visibleLines = Height / Font.Height;
            int totalLines = GetLineCount();
            
            if (totalLines <= visibleLines)
                return Rectangle.Empty; // 不需要滚动条
            
            // 计算滑块高度比例
            float thumbHeightRatio = (float)visibleLines / totalLines;
            int thumbHeight = Math.Max((int)(scrollBarRect.Height * thumbHeightRatio), 20); // 最小高度为20像素
            
            // 计算滑块位置
            int scrollPosition = GetScrollPosition();
            float scrollRatio = (float)scrollPosition / (totalLines - visibleLines);
            int thumbTop = scrollBarRect.Top + (int)((scrollBarRect.Height - thumbHeight) * scrollRatio);
            
            return new Rectangle(
                scrollBarRect.X + 1, 
                thumbTop, 
                scrollBarRect.Width - 2, 
                thumbHeight);
        }

        private int GetLineCount()
        {
            // 获取文本总行数
            return Text.Split('\n').Length;
        }

        private int GetScrollPosition()
        {
            // 获取当前滚动位置
            return Math.Max(0, GetFirstVisibleLine());
        }

        [DllImport("user32.dll")]
        private static extern int GetScrollPos(IntPtr hWnd, int nBar);

        [DllImport("user32.dll")]
        private static extern int SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);

        private int GetFirstVisibleLine()
        {
            // 获取第一个可见行
            return SendMessage(Handle, 0x00CE, 0, 0); // EM_GETFIRSTVISIBLELINE
        }
    }
} 